package com.example.jdbcdemo.controller;


import com.example.jdbcdemo.mapper.UserMapper;
import com.example.jdbcdemo.pojo.User;
import com.example.jdbcdemo.service.UserServiceInterface;
import com.example.jdbcdemo.utils.JsonUtil;
import com.example.jdbcdemo.utils.LoginJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserServiceInterface userServiceInterface;


    //用于登录界面获取json数据
    @PostMapping("/login")
    @ResponseBody
    public LoginJsonUtil getUser(@RequestBody HashMap<String,String> map){
        return userServiceInterface.queryUserLoginJson(map.get("username"),map.get("password"));
    }

    //注册新用户获取json数据
    @PostMapping("/signup")
    @ResponseBody
    public JsonUtil newUser(@RequestBody HashMap<String,String> map){
        //数据库中主键自增，所以id随便起
        return userServiceInterface.queryUserSignUpJson(map.get("username"),map.get("password"), Integer.parseInt(map.get("usertype").toString()));
    }

    //实现注销功能
    @PostMapping("/logout")
    @ResponseBody
    public JsonUtil logout(HttpSession session){
        session.invalidate();
        return new JsonUtil(0,"注销成功","");
    }

    @PostMapping("/user/userList")
    public JsonUtil userList(){
        return userServiceInterface.queryUserList();
    }
}
